<?php


  include('../includes/configure.php');
  include('../includes/functions/database.php');
	$Message = "CPI complete received\n\noid=$oid\n\n";
	
	if (is_array ($_REQUEST))
	{
		$Message .= "Posted variables\n";
		reset ($_REQUEST);
		while (list ($key, $val) = each ($_REQUEST)) {
			if (is_array ($val))
			{
				reset ($val);
				while (list ($k2, $v2) = each ($val)) {
					$Message .= "$key $k2 => $v2\n";
				}
				
			} else
			{
			
				$Message .= "$key => $val\n";
			}
		}
	}

	if (is_array ($_REQUEST))                                             //not the case
	{
		$Message .= "\n\nGet variables\n";
		reset ($_REQUEST);
		while (list ($key, $val) = each ($_REQUEST)) {
			if (is_array ($val))
			{
				reset ($val);
				while (list ($k2, $v2) = each ($val)) {
					$Message .= "$key $k2 => $v2\n";
				}
			} else
			{
				$Message .= "$key => $val\n";
			}
		}
	}                                                                           //not the case
	$oid = trim($_REQUEST['oid']);
	if (!($cid = strtok($oid,'-')))
	{	
		$cid = $oid;
	}
	$cid = addslashes($cid);
	$status = addslashes($_REQUEST['transactionstatus']);
	if ($cid == "")
	{
		$t = time();
		$d = date ("d-M-y H:i:s",$t);
		$cid = "bad".$d;
		$status = "manual call";
	}
 

    
    
	$selstr = "INSERT INTO epdq_transactions (oid, transaction_status, transaction_time) VALUES ('$oid','$status',NOW());";
    $Message .= "Connect to db ".DB_DATABASE."\nServer ".DB_SERVER."\nU: ".DB_SERVER_USERNAME."\nP: ".DB_SERVER_PASSWORD."\n";

   	$link = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD);
    if ($link) mysql_select_db(DB_DATABASE);
    tep_db_connect();

    if (($cid != "") && ($status != ""))
 	{
        if($status == "Success")
        {
            $new_status = 5;   // [SEE HERE] Depends on your status setup
        }
        else
        {
            $new_status = 6;   // [SEE HERE] Depends on your status setup
        }
        $selstr2 = "UPDATE orders SET orders_status = '$new_status', last_modified = NOW() WHERE orders_id = '$oid'";

        //Make new insert into status history
        $sql_data_array = array('orders_id' => $oid,
                                'orders_status_id' => $new_status,  //4 is Awaiting Card Transaction
                                'date_added' => 'now()',
                                'customer_notified' => '0',
                                'comments' => $order->info['comments']);
        tep_db_perform('orders_status_history', $sql_data_array);

 //       tep_db_query($selstr);

		$result = mysql_query($selstr, $link);
        //Update order status to payment received but only if transaction status is success
        if($status == "Success")
        {
            $result2 = mysql_query($selstr2, $link);
        }
        else
        {
            $result2 = "Not updated, transaction not successful. Please follow up.";
        }
		$err = mysql_error();
		$Message .= "cid = $cid\n$selstr\nResult1 = $result\n$selstr2\nResult2 = $result2\nErr = $err\n";
		mail ("you@youraddress.co.uk","BAM EPDQ Transaction", $Message, "From: webrobot \nReply-To: \nX-Script-Author: Brian Burton-Cundall\nX-ip:$REMOTE_ADDR\nX-Mailer: PHP/" . phpversion()); // [SEE HERE - ePDQ] Change e-mail address for debugging

	} else
	{
		tep_db_connect();

//        tep_db_query($selstr);
		$result = mysql_query($selstr, $link);
		$err = mysql_error();
		$Message .= "cid = $cid\n$selstr\n$err\n";
		mail ("you@youraddress.co.uk","BAM EPDQ Transaction", $Message, "From: webrobot \nReply-To: \nX-Script-Author: Brian Burton-Cundall\nX-ip:$REMOTE_ADDR\nX-Mailer: PHP/" . phpversion()); // [SEE HERE - ePDQ] Change e-mail address for debugging


		print "Called manually - error reported to webmaster";
	}

?>
